﻿using System.Net.Sockets;
using System.Net;
using System;
using System.Data;
namespace PssFramework
{

    namespace PSClient
    {
        public class Publisher
        {
            Socket _client;
            EndPoint _remoteEndPoint;

            public Publisher()
            {

                IPAddress serverIPAddress = IPAddress.Parse(Config.getInstance().Ip);
                _client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _remoteEndPoint = new IPEndPoint(serverIPAddress, Config.getInstance().PublishPort);
            }


            public void WriteEvent(int Topic, int Key, byte[] Data)
            {
                int cmd = (int)PssFramework.EPPSCommands.PUBLISH;
                int[] header = new int[] { cmd, Topic, Key };
                byte[] txData = new byte[Data.Length + header.Length*4];
                int offset = 12;// header.Length * 4;
                Buffer.BlockCopy(header, 0, txData, 0, offset);
                Buffer.BlockCopy(Data, 0, txData, offset, Data.Length);
                _client.SendTo(txData, _remoteEndPoint);
            }
        }
    }
}